#include "compile_run.hpp"
#include "../comm/httplib.h"

using namespace ns_compiler_and_run;
using namespace httplib;
void Usage(std::string proc)
{
  std::cerr<<"Usage:"<<"\n\t"<<proc<<"port"<<std::endl;
}

int main(int argc,char *argv[])
{
  if(argc!=2)
  {
   Usage(argv[0]);
  }
  httplib::Server svr;
  svr.Get("/hi",[](const Request &req,Response &resp){
    resp.set_content("你好,世界!","text/plain;charset=utf-8");
  });
  svr.Post("/compile_run",[](const Request &req,Response &resp){
    std::string in_json=req.body;
    std::string out_json;
    if(!in_json.empty())
    {
      CompileAndRun::Start(in_json,&out_json);
      resp.set_content(out_json,"application/json;chatset=utf-8");
    }
  });
  //svr.set_base_dir("./wwwroot")
  svr.listen("0.0.0.0",atoi(argv[1]));
  return 0;

    /*std::string in_json;AQ
    Json::Value in_value;
    in_value["code"]=R"(
    #include<iostream>
    int main()
    {
        std::cout<<"这里是代码"<<std::endl;
      int *a[]=new 
        return 0;
    }
    )";
    in_value["input"]="";
    in_value["cpu_limit"]=1;
    in_value["mem_limit"]=10240*3;
    Json::FastWriter writer;
    in_json=writer.write(in_value);
    std::cout<<in_value<<std::endl;
    std::string out_json;
   CompileAndRun::Start(in_json,&out_json);
   std::cout<<out_json<<std::endl;
    return 0;
    */
}
   